﻿var dataLoaiSanPham = [];
var dataDonViTinh = [];
function getLoaiSanPham(newValue) {
    // Loại sản phẩm
    var d = $.Deferred();
    $.ajax({
        type: "POST",
        url: '/Ajax/DanhMuc.ashx?command=loaisanpham&HinhThucQuangCaoID=' + newValue,
        dataType: 'json',
        success: function (data) {
            if (typeof data == 'string')
                data = JSON.parse(data);
            if (data.Success) {
                while (dataLoaiSanPham.length > 0)
                    dataLoaiSanPham.pop();
                $.each(data.Data, function (i, v) {
                    dataLoaiSanPham.push({ id: v.SanPhamID, text: v.TenSanPham });
                });
                console.log('getLoaiSanPham');
                d.resolve();
            }
            console.log('1 :' + dataLoaiSanPham);
        }
    });
    return d.promise();
}

function getDonViTinh(newValue) {
    // Don Vi Tinh
    $.ajax({
        type: "POST",
        url: '/Ajax/DanhMuc.ashx?command=donvitinh&donvitinhID=' + newValue,
        dataType: 'json',
        success: function (data) {
            if (typeof data == 'string')
                data = JSON.parse(data);
            if (data.Success) {
                $.each(data.Data, function (i, v) {
                    dataDonViTinh.push({ id: v.DonViTinhID, text: v.TenDonViTinh });
                });
                console.log('getDonViTinh');
            }
        }
    });
}

$(document).ready(function () {

    $('#table-hdct tbody').on('click', 'tr', function () {
        $('#table-hdct tbody tr.selected').removeClass('selected');
        $(this).addClass('selected');
    });

    // Hình thức quảng cáo
    $('.cls-hinhthucquangcao').editable({
        type: 'select2',
        showbuttons: false,
        emptytext: '',
        pk: 1,
        select2: {
            placeholder: '',
            width: '230px',
            minimumResultsForSearch: "-1",
            ajax: {
                url: '/Ajax/DanhMuc.ashx?command=hinhthucquangcao',
                dataType: 'json',
                results: function (data, page) {
                    var labels = $.map(data.Data, function (item) { return { id: item.HinhThucQuangCaoID, text: item.TenHinhThucQuangCao }; });
                    return { results: labels };
                }
            },
        },
        params: function (params) {
            params.state = $(this).editable('getValue');
        },
        success: function (response, newValue) {


        }
    });

    $('.cls-loaisp').editable({
        type: 'select2',
        showbuttons: false,
        emptytext: '',
        source: function () { return dataLoaiSanPham; },
        pk: 1,
        select2: {
            placeholder: '',
            width: '230px',
            minimumResultsForSearch: "-1"
        },
        success: function (response, newValue) {
            alert(newValue);

        }
    });
    var count = 0;
    $('.cls-loaisp').on("shown", function (e, editable) {

        if ($(this).closest('tr').find('td.cls-hinhthucquangcao').html() == 'CPM') {
            $.when(
              getLoaiSanPham(6)
           ).done(function () {
               $(this).editable('option', 'source', dataLoaiSanPham);

           });
        }
        else {
            $.when(
              getLoaiSanPham(14)
           ).done(function () {
               $(this).editable('option', 'source', dataLoaiSanPham);
           });
        }
    });
});

